package com.backstage.controller;

import com.backstage.common.annotation.log.Operate;
import com.backstage.common.base.bean.Message;
import com.backstage.common.page.Page;
import com.backstage.common.uitil.JsonUtil;
import com.backstage.entity.Menu;
import com.backstage.entity.Role;
import com.backstage.service.RoleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Project authority
 * @Package com.backstage.controller
 * @Data 2017/7/27下午 3:47.
 * @Aurhor 阮雪峰
 */
@RequestMapping("/role")
@Controller
public class RoleController extends BaseController {

    @Resource(name="roleService")
    private RoleService roleService;

    /**
     * 通过分页查询角色
     * @param page
     * @param role
     * @return
     */
    @RequiresPermissions("sys:role:list")
    @RequestMapping("/queryByPage")
    @ResponseBody
    public Page<Role> queryByPage(Page<Role> page, Role role){
        roleService.roleByPage(page, role);
        return page;
    }

    /**
     * 新增
     * @param role
     * @return
     */
    @Operate("菜单管理：新增")
    @RequiresPermissions("sys:role:add")
    @RequestMapping("/add")
    @ResponseBody
    public Message add(Role role){
        role.setCreator(this.getLoginUser().getUid());
        roleService.add(role);
        return Message.ADD_SUCCESS;
    }

    /**
     * 通过id查询角色
     * @param role
     * @return
     */
    @RequestMapping("/getRoleById")
    @ResponseBody
    public Map<String, Object> getRoleById(Role role) throws JsonProcessingException {
        return JsonUtil.objectToMap(roleService.queryRoleById(role));
    }

    /**
     * 修改
     * @param role
     * @return
     */
    @Operate("菜单管理：修改")
    @RequiresPermissions("sys:role:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public Message edit(Role role){
        role.setEditor(this.getLoginUser().getUid());
        roleService.edit(role);
        return Message.EDIT_SUCCESS;
    }

    /**
     * 删除
     * @param roles
     * @return
     */
    @Operate("菜单管理：删除")
    @RequiresPermissions("sys:role:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestBody ArrayList<Role> roles){
        roleService.delete(roles);
        return Message.DELETE_SUCCESS;
    }

    /**
     * 查询角色权限
     * @param role
     * @return
     */
    @RequestMapping("/getRoleMenu")
    @ResponseBody
    public Map<String, List<Menu>> getRoleMenu(Role role){
        return roleService.getRoleMenu(role);
    }

    /**
     * 分配权限
     * @param role
     * @return
     */
    @Operate("菜单管理：分配权限")
    @RequiresPermissions("sys:role:allotPower")
    @RequestMapping("/allotPower")
    @ResponseBody
    public Message allotPower(Role role){
        role.setCreator(this.getLoginUser().getUid());
        roleService.allotPower(role);
        return Message.OPERATE_SUCCESS;
    }
}
